package ylyhappy.cn.ideavim.ui.configpreview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import ylyhappy.cn.ideavim.R;

public class ConfigItemAdapter extends RecyclerView.Adapter<ConfigItemAdapter.MyViewHolder> {
    private final List<String> mList;
    private final Context mContext;
    private OnItemClickListener mOnItemClickListener;

    public ConfigItemAdapter(List<String> mList, Context mContext) {
        this.mList = mList;
        this.mContext = mContext;
    }

    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public void removeData(int position) {
        mList.remove(position);
        notifyItemRemoved(position);
    }

    @NonNull
    @Override
    public ConfigItemAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.config_item_recycler, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ConfigItemAdapter.MyViewHolder holder, int position) {
        holder.tv.setText(mList.get(position));

        if (mOnItemClickListener != null) {
            holder.cb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(holder.cb, pos);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }



    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv;
        CheckBox cb;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv_item);
            cb = itemView.findViewById(R.id.config_item_checkbox);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

//        void onItemLongClick(View view, int position);
    }

}
